/*KypxrnTlCsNW6raC5wgkoL6MjbkvcYxunKIWAbvKVu4=*/
//主子表卡片
import React, { Component } from 'react';
import { createPage, ajax, base, getMultiLang,getParentWindow, getTopWindow } from 'nc-lightapp-front';
import { formBeforeEvent } from '../../../public/components/pubUtils/arapFormRefFilter';
import { bodyBeforeEvent } from '../../../public/components/pubUtils/arapTableRefFilter';
import { cardFieldsEditableWithProxy } from '../../../public/components/pubUtils/billFieldEditableUtil.js';
let { NCButton, NCButtonGroup } = base;
import { initTemplate, afterEvent, bodyBeforeEventInner } from './events';
let arr = [
	'MONEY_DE',
	'LOCAL_MONEY_DE',
	'QUANTITY_DE',
	'MONEY_BAL',
	'LOCAL_MONEY_BAL',
	'QUANTITY_BAL',
	'LOCAL_TAX_DE',
	'NOTAX_DE',
	'LOCAL_NOTAX_DE',
	'GROUPDEBIT',
	'GROUPTAX_DE',
	'GROUPNOTAX_DE',
	'GLOBALDEBIT',
	'GLOBALTAX_DE',
	'GLOBALNOTAX_DE',
	'QUANTITY_CR',
	'LOCAL_MONEY_CR',
	'MONEY_CR',
	'LOCAL_TAX_CR',
	'NOTAX_CR',
	'LOCAL_NOTAX_CR',
	'GROUPCREBIT',
	'GROUPTAX_CRE',
	'GROUPNOTAX_CRE',
	'GLOBALCREBIT',
	'GLOBALTAX_CRE',
	'GLOBALNOTAX_CRE',
	'OCCUPATIONMNY',
	'PRICE',
	'GROUPBALANCE',
	'GLOBALBALANCE',
	'TAXPRICE',
	'CALTAXMNY',
	'NOSUBTAX',
	'MONEY',
	'GROUPLOCAL',
	'GLOBALLOCAL',
	'PK_CURRTYPE',
	'PK_TRADETYPE',
	'PK_TRADETYPEID',
	'PK_BILLTYPE',
	'RATE',
	'GROUPRATE',
	'GLOBALRATE'
];

//大写转小写方法
let ToLowerCase = () => {
	return arr.map((item, index, array) => {
		return item.toLowerCase();
	});
};
let brr = ToLowerCase();
let billType;

class Card extends Component {
	constructor(props) {
		super(props);
		this.formId = 'head';
		this.tableId = 'bodys';
		this.pageId = this.props.getUrlParam('tradeType');
		this.state = {
			json: {}
		};
		this.handleConfirm = this.handleConfirm.bind(this);
		this.handleCancel = this.handleCancel.bind(this);
		props.use.form(this.formId);
		props.use.cardTable(this.tableId)
	}
	componentWillMount() {
		let callback = (json) => {
			this.setState({ json: json });
		};
		getMultiLang({
			moduleId: ['verificationsheet', 'public'],
			domainName: 'arap',
			currentLocale: 'simpchn',
			callback
		});
	}
	componentDidMount() {
		initTemplate.call(this, this.props);
		this.getDatas();
		billType = this.props.getUrlParam('billType');
	}

	getDatas = () => {
		let data = this.props.ViewModel.getData('transToCardDatas').JumpcardDatas;
		ajax({
			url: '/nccloud/arap/billverify/varyMoney.do',
			data: data,
			success: (res) => {
				if (res.data) {
					if (res.data.head) {
						this.props.form.setAllFormValue({ [this.formId]: res.data.head[this.formId] });
						this.props.form.setFormItemsDisabled(this.formId, {
							pk_org: true,
							pk_currtype: true,
							approvestatus: true,
							approvedate: true,
							approvedated: true,
							billstatus: true,
							effectstatus: true,
							effectuser: true,
							creator: true,
							effectdate: true,
							src_syscode: true,
							pk_org_v: true,
							rate: true,
							globalrate: true,
							grouprate: true
						});
					}
					if (res.data.body) {
						this.props.cardTable.setStatus(this.tableId, 'edit');
						this.props.cardTable.setTableData(this.tableId, res.data.body[this.tableId]);
						cardFieldsEditableWithProxy(this)
						setTimeout(() => {
							this.props.cardTable.setColEditableByKey(this.tableId, brr, true);
						}, 500);
					}
				} else {
					this.props.form.setAllFormValue({ [this.formId]: { rows: [{ values: {} }] } });
					this.props.cardTable.setTableData(this.tableId, { rows: [{ values: {} }] });
				}
			}
		});
	};

	//确定按钮
	handleConfirm = () => {
		//不等于暂存的时候开启检验
		let result=this.props.validatePageToToast([
			{
				name: this.formId,
				type: "form",
			},
			{
				name: this.tableId,
				type: "cardTable",
			}
		]);
		if(result&&!result.allPassed){
			return;
		}
		let transferCardData = this.props.ViewModel.getData('transToCardDatas').transferCardDatas;
		let WholeVerifyDatas = this.props.ViewModel.getData('transToCardDatas').transferData;
		let id = WholeVerifyDatas.pk_bill == null ? null : WholeVerifyDatas.pk_bill.value;
		let data = {
			cardData: this.props.createMasterChildData(this.pageId, this.formId, this.tableId),
			...transferCardData
		};
		let scene = this.props.getUrlParam('scene');
		let sence = 1;
		if (scene == 'approve' || scene == 'approvesce' || scene == 'zycl') {
			sence = 3;
		}
		ajax({
			url: '/nccloud/arap/billverify/varyMoneyConfirm.do',
			data: data,
			success: (res) => {
				ajax({
					url: '/nccloud/arap/arappub/linkarapbill.do',
					async: false,
					data: {
						pk_bill: WholeVerifyDatas.pk_bill == null ? null : WholeVerifyDatas.pk_bill.value,
						tradeType: WholeVerifyDatas.pk_tradetype.value,
						sence: sence
					},
					success: (res) => {
						this.props.setUrlParam({ status: 'browse' });
						if (getTopWindow() === getParentWindow(window)) {
							// this.props.pushTo('/card', {
							// 	pagecode: this.props.getUrlParam('pageCode'),
							// 	appcode: this.props.getUrlParam('appCode'),
							// 	id: id,
							// 	scene: this.props.getUrlParam('scene'),
							// 	status: 'browse'
							// });

						//单据保存之后转到单据卡片页面
						this.props.pushTo('/card', {
							pagecode: res.data.condition.pagecode,
							appcode: res.data.condition.appcode,
							id: id,
							scene: this.props.getUrlParam('scene'),
							status: 'browse'
						});

						} else {
							let parameter =
								'pagecode=' +
								res.data.condition.pagecode +
								'&appcode=' +
								res.data.condition.appcode +
								'&scene=' +
								scene +
								'&p=' +
								res.data.condition.pagecode +
								'&c=' +
								res.data.condition.appcode +
								'&id=' +
								id +
								'&status=browse';
							window.location.hash = '/card?' + parameter;
							window.parent.location.hash = '/card?' + parameter;
						}
					}
				});
			}
		});
	};

	//取消按钮
	handleCancel = () => {
		let WholeVerifyDatas = this.props.ViewModel.getData('transToCardDatas').transferData;
		this.props.ViewModel.setData('WholeVerifyDatas', WholeVerifyDatas);
		let nowv_pc = '20060NOWV';
		let ac = '20062002';
		if (billType == 'F1' || billType == 'F3') {
			nowv_pc = '20080NOWV';
			ac = '20082002';
		}
		if (getTopWindow() === getParentWindow(window)) {
			this.props.pushTo('/nowv', {
				pagecode: nowv_pc,
				appcode: ac
			});
		} else {
			let scene = this.props.getUrlParam('scene');
			let parameter =
				'pagecode=' +
				this.props.getUrlParam('pageCode') +
				'&appcode=' +
				this.props.getUrlParam('appCode') +
				'&scene=' +
				scene +
				'&p=' +
				this.props.getUrlParam('pageCode') +
				'&c=' +
				this.props.getUrlParam('appCode');
			window.location.hash = '/nowv?' + parameter;
			window.parent.location.hash = '/nowv?' + parameter;
		}
	};
	bodyBeforeEventInner = (props, moduleId, key, value, index, record, type) => {
		bodyBeforeEvent.call(this, props, moduleId, key, value, index, record, type)
		if (type == 'model') {
			moduleId = "bodys_edit"
		}
		var meta = props.meta.getMeta();
		var tradeType = record.values.pk_tradetype ? record.values.pk_tradetype.value : null;
		var tableObject = {
			model: {
				areaType: "table",
				areacode: null,
				rows: [record]
			},
			pageid: this.pageId
		}
		let formObject = {
			model: this.formId ? props.form.getAllFormValue(this.formId) : null,
			pageid: this.pageId
		}
		let formData = JSON.stringify(formObject);
		let tableData = JSON.stringify(tableObject);
		var config = {
			itemKey: '',
			isHead: false,
			crossRuleConditionsVO: formData,//表单的内容
			crossRuleTableConditionsVO: tableData,//表格的内容
			tradeType: tradeType,//交易类型
			DataPowerOperationCode: '',//使用权
			isDataPowerEnable: 'Y',//使用权限
			pk_org: '',
			busifuncode: '',
			AppCode: '',
			orgType: '',
			refnodename: '',//参照显示名称
			accclass: '',
			pk_cust: '',
			pk_currtype: '',//货币类型
			pkOrgs: '',
			parentbilltype: '',
			datestr: '',//业务日期
			pk_accountingbook: '',//会计主账簿的id
			dateStr: '',
			GridRefActionExt: "",
			TreeRefActionExt: "",
			supplier: '',
			customer: '',
			pretype: '',//收付款性质 
			customSupplier: '',//散户
			pk_psndoc: '',//个人银行账户
			profitcenter: '',//利润中心
			financeorg: '',
			isShowDisabledData: '',
			index: index
		}
		meta[moduleId].items.map((item) => {
			item.isShowUnit = false;
			item.isShowDisabledData = false;
			var attrcode = item.attrcode;
			if (item.itemtype == 'refer') {
				item.isMultiSelectedEnabled = false;
				item.unitValueIsNeeded = false; //参照item.unitValueIsNeeded为true时：只有选业务单元才会发请求
			}
			if (attrcode == key) {
				switch (attrcode) {
					case 'pk_recpaytype'://收款业务类型
						item.queryCondition = (p) => {
							if (p) {
								if (p.refType == 'grid' || p.refType == 'gridTree') {
									config.GridRefActionExt = 'nccloud.web.arap.ref.before.RecPayTypeSqlBuilder';
								} else if (p.refType == 'tree') {
									config.TreeRefActionExt = 'nccloud.web.arap.ref.before.RecPayTypeSqlBuilder';
								}
							} else {
								config.GridRefActionExt = 'nccloud.web.arap.ref.before.RecPayTypeSqlBuilder';
							}
							config.UsualGridRefActionExt = 'nccloud.web.arap.ref.before.RecPayTypeSqlBuilder';
							config.itemKey = key;
							config.DataPowerOperationCode = 'fi';//使用权组
							config.pk_org = record.values.pk_org.value;
							config.VersionStartDate = props.form.getFormItemsValue(this.formId, 'billdate') ? props.form.getFormItemsValue(this.formId, 'billdate').value : null;
							return config;
						}
						break;
				}
			}
		})
		props.meta.setMeta(meta);
	}

	render() {
		let { cardTable, form } = this.props;
		let { createForm } = form;
		const { createBillHeadInfo } = this.props.BillHeadInfo;
		let { createCardTable } = cardTable;
		return (
			<div className="nc-bill-card">
				<div className="nc-bill-top-area">
					<div className="nc-bill-header-area">
						<div className="header-title-search-area">
							{createBillHeadInfo({
								title: billType == 'F0' ? this.state.json['verificationsheet-000040'] : billType == 'F1' ? this.state.json['verificationsheet-000041'] : billType == 'F2' ? this.state.json['verificationsheet-000044'] : billType == 'F3' ? this.state.json['verificationsheet-000043'] : '',//国际化处理： 应收期初
								initShowBackBtn: false
							})}
						</div>
						<div class="header-button-area">
							<NCButton fieldid="sure" colors="primary" onClick={this.handleConfirm}>{this.state.json['verificationsheet-000103']}</NCButton>
							{/* 国际化处理： 确定*/}
							<NCButton fieldid="cancel" onClick={this.handleCancel}>{this.state.json['verificationsheet-000049']}</NCButton>
							{/* 国际化处理： 取消*/}
						</div>
					</div>
					<div className="nc-bill-form-area">
						{createForm(this.formId, {
							fieldid: "verifynow",
							onBeforeEvent: formBeforeEvent.bind(this),
							onAfterEvent: afterEvent.bind(this, billType)
						})}
					</div>
				</div>
				<div className="nc-bill-bottom-area">
					<div className="nc-bill-table-area">
						{createCardTable(this.tableId, {
							fieldid: "verifynow",
							onBeforeEvent: this.bodyBeforeEventInner.bind(this),
							onAfterEvent: afterEvent.bind(this, billType),
							adaptionHeight: true
						})}
					</div>
				</div>
			</div>
		);
	}
}

Card = createPage({
	orderOfHotKey: ['head', 'bodys'],//区域快捷键切换支持
})(Card);

export default Card;

/*KypxrnTlCsNW6raC5wgkoL6MjbkvcYxunKIWAbvKVu4=*/